package SEApp
{
	import UIFree.controls.uif_gbutton;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	
	public class se_util {
		public static var DEFWIDTHBUTTON: Number = 120;
		public static var DEFHEIGHTBUTTON: Number = 30;
		public static const MAXBITMAPSIZE: uint = 16777215;
		
		public static function userName(userData: *): String {
			if (userData) {
				var name: String = userData.first_name + ' ' + userData.last_name;
				if (!name) name = userData.nick;
				return name;
			}
			return '';
		}
		
		public static function albumsPrepare(albums: Array, privacy: Boolean = true): Array {
			var notIDs : Array = ['_mydialog', '_myappwidgets', '_cards'];
			var result: Array = new Array();
			for each (var album: Object in albums) {
				if ((notIDs.indexOf(album.aid) == -1) &&
					(!privacy || !album.privacy
					|| (album.privacy == 2)	// открыт для всех; 
					|| (album.privacy == 4)	// только для друзей; 
					|| (album.privacy == 5) // только для лучших друзьям; 
					) && (album.size > 0)
					&& (album.aid != '_myappwidgets')) {
						album.smallTitle = Utils.ll(album.title?album.title:album.created, 16);		
						result.push(album);
					}
			}
			return result;
		}
		
		public static function bmpCopy(_source: DisplayObject, scale: Number=1, target: Bitmap=null): Bitmap {
			var sourceScale: Number = 1;
			var totalPix: uint = _source.width * _source.height;
			if (totalPix > MAXBITMAPSIZE) sourceScale = MAXBITMAPSIZE/totalPix;
			
			var smat: Matrix = new Matrix();
			smat.scale(sourceScale, sourceScale);
			
			var tmp: Bitmap = new Bitmap(new BitmapData(_source.width * sourceScale, _source.height * sourceScale, true, 0x00FFFFFF));
			tmp.bitmapData.draw(_source, smat);
			
			if (scale != 1) tmp.smoothing = true;
			
			if (target == null) 
				target = new Bitmap();
			else {
				if (target.bitmapData) target.bitmapData.dispose();
			}
			target.bitmapData = new BitmapData(tmp.bitmapData.width * scale, tmp.bitmapData.height * scale, true, 0x00FFFFFF);
			
			var mat: Matrix = new Matrix();
			if (scale != 1) mat.scale(scale, scale);
			target.bitmapData.draw(tmp, mat, null, null, null, true);
			
			tmp.bitmapData.dispose();
			return target;			
		}
		
/*		public static function encode(str: String): String {
			var i	: int = 0;
			var data: String = '';
			while (i < str.length) {
				var code: String = str.charCodeAt(i).toString(16);
				data += (code.length < 3)?('0' + code):code;
				i++;
			}
			return data;
		}
		
		public static function decode(str: String): String {
			var i	: int = 0;
			var data: String = '';
			while (i < str.length) {
				data += str.charCodeAt(i).toString(16);
				i += 2;
			}
			return data;
		}*/
		
		public static function bmpEncode(_source: BitmapData): String {
			var data: String = '';
			var ba: ByteArray = _source.getPixels(_source.rect);
			var i:int = 0;
			while (i < ba.length) {
				if (i % 4 != 0) {
					var code: String = ba[i].toString(16);
					data += (code.length < 2)?('0' + code):code;
				}
				i++;
			}
			return _source.width + 'x' + _source.height + '|' + data;
		} 
		
		public static function createComponent(parent: uif_component, componentClass: Class, styleClass: String=''): uif_component {
			var inner: Rectangle = parent.getInner();
			return new componentClass(parent, inner.x, inner.y, inner.width, inner.height, styleClass);
		}
		
		public static function defButton(parent: uif_ecomponent, offsetX: int, offsetY: int, caption: String='ok', 
										releaseFunc: Function=null, hint: String=''): uif_gbutton {
			var inner: Rectangle = parent.getInner();
			var button: uif_gbutton = new uif_gbutton(parent, inner.x + offsetX, inner.y + offsetY, DEFWIDTHBUTTON, DEFHEIGHTBUTTON, 'button');
			button.text = caption;
			if (releaseFunc != null) button.addEvent(MouseEvent.CLICK, releaseFunc);
			button.hint = hint;
			return button;
		}
		
// Устанавливает значение по полу в строке:
//"Ты [male:самый лучший][fale:самая лучшая] человек из всех [male:мужчин][fale:женщин]"
		public static function MFReplace(text: String, male: Boolean = true, friend: Boolean = true): String {
			var maleA: Array;
			var femaleA: Array
			if (friend) {
				maleA = text.match(/\[mf:[\w\sА-я]+\]/g);
				femaleA = text.match(/\[ff:[\w\sА-я]+\]/g);
			} else {
				maleA = text.match(/\[mu:[\w\sА-я]+\]/g);
				femaleA = text.match(/\[fu:[\w\sА-я]+\]/g);
			}
			
			function replaceIn(arr: Array, clear: Boolean): void {
				for (var i:int=0; i<arr.length; i++) {
					if (clear) text = text.replace(arr[i], '');
					else {
						var inText: String = arr[i].substr(4, arr[i].length - 5);
						text = text.replace(arr[i], inText);
					}
				}
			}
			
			replaceIn(maleA, !male);
			replaceIn(femaleA, male);
			return text;
		}
		
		public static function getUids(list: Array, idField: String='uid'): String {
			var uids: Array = new Array();
			list.forEach(function(item: Object, index: int, arr: Array): void {
				if (uids.indexOf(item[idField]) == -1) uids.push(item[idField]);
			});
			
			return uids.toString();
		}
	}
}